<?php
include('includes/config.php');
if ( $_POST ) {
      $title = filter_var(trim($_POST['title']), FILTER_SANITIZE_STRING);
      $text = filter_var($_POST['text'], FILTER_SANITIZE_STRING);
      $textTrimed = trim($text);
      $error = false;

      if ( empty($title) || mb_strlen($title) > 50 ) {
            $errorMessages[] = 13;
            $error = true;
      }

      if ( empty( $textTrimed ) || mb_strlen( $textTrimed ) > 250 ) {
            $errorMessages[] = 14;
            $error = true;
      }
      
      if ( !$error ) {
            $mID = addMessage($title, $text, $_SESSION['user_id'], $link);
            
            if ( $mID ) {
                  header("Location: msglist.php?mid=$mID");
                  exit;
            }
            else {
                  $errorMessages[] = 16;
            }
      }
}

$pageTitle = 'Добавяне на ново съобщение';
include 'includes/header.php';
?>                   
<div class="menu">
      <a href="msglist.php">Списък с всички съобщения</a>
      <a href="addmessage.php" class="current">Ново съобщение</a> 
      <a href="index.php?do=login">Изход</a> 
      <span class="welcome tar floatRight"><?php echo welcomeMessage(); ?></span>
</div>
<?php
include 'includes/messages.php';
?>
<div id="container">
      <form action="" method="POST">
            <table class='noBorders'>
                  <tr>
                        <td>
                              <label for='title'>Заглавие до 50 символа : </label>
                              <input type="text" name="title" id="title" value='<?php echo $_POST['title']; ?>'>
                              <br /><span id='titleLeft'></span>

                        </td>
                  </tr>
                  <tr>
                        <td><label for='text'>Съобщение до 250 символа :</label>
                              <textarea name='text' id='text' class='wf' rows='3'><?php echo $_POST['text']; ?></textarea>
                              <br /><span id='textLeft'></span>
                        </td>
                  </tr>
                  <tr>
                        <td><br /><input type="submit" value="Изпрати" /></td>
                  </tr>
            </table>
      </form>
</div>
<?php
      include 'includes/footer.php';